#!/bin/bash

# while
var1=10
while [ $var1 -gt 0 ]; do
    echo $var1
    var1=$[ $var1 - 1 ]
done

# until
var1=100
until [ $var1 -eq 0 ]; do
    echo $var1
    var1=$[ $var1 - 25 ]
done

# break
for (( a = 1; a < 4; a++ )); do
    echo "Outer loop: $a"
    for (( b = 1; b < 100; b++ )); do
        if [ $b -eq 5 ]; then
            break
        fi
        echo "Inner loop: $b"
    done
done
# break outer loop
for (( a = 1; a < 4; a++ )); do
    echo "Outer loop: $a"
    for (( b = 1; b < 100; b++ )); do
        if [ $b -eq 5 ]; then
            break 2
        fi
        echo "Inner loop: $b"
    done
done

# continue outer loop
for (( a = 1; a <= 5; a++ )); do
    echo "Iteration $a:"
    for (( b = 1; b < 3; b++ )); do
        if [ $a -gt 2 ] && [ $a -lt 4 ]; then
            continue 2
        fi
        var3=$[ $a * $b ]
        echo "  The result of $a * $b is $var3"
    done
done

# redirect, can also be piped
testfile=testloop.txt
for (( a = 1; a < 10; a++ )); do
    echo "The number is $a"
done > $testfile
echo "The command is finished."
cat $testfile
rm $testfile

